/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package han.bifang.gwt.client;

import han.bifang.gwt.client.common.ContentChangeListener;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.communicate.BifangGWTService;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;

import java.util.HashMap;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Catalog implements EntryPoint, ContentChangeListener {

	private CatalogMessages constants = null;
	private GwtContext context = null;
	private BifangGWTServiceAsync service;
	private SimplePanel panel;

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		constants = GWT.create(CatalogMessages.class);
		// VerticalPanel vPanel = new VerticalPanel();
		// vPanel.setWidth("100%");
		// vPanel.setHorizontalAlignment(VerticalPanel.ALIGN_LEFT);
		// TabPanel tabs = new TabPanel() {
		// public boolean onBeforeTabSelected(SourcesTabEvents sender,
		// int tabIndex) {
		// if (tabIndex == 2)
		// serviceTab.init();
		// return true;
		// }
		// };
		// tabs.setWidth("100%");
		context = new GwtContext();

		service = (BifangGWTServiceAsync) GWT.create(BifangGWTService.class);
		ServiceDefTarget endpoint = (ServiceDefTarget) service;
		endpoint.setServiceEntryPoint(GWT.getModuleBaseURL() + "/bifangGWT");

		//
		// DomainTab domaintab = new DomainTab(service, context);
		// domaintab.setWidth("100%");
		//
		// tabs.add(domaintab, constants.domainMgmt());
		// tabs.selectTab(0);
		//
		// ApplicationTab appTab = new ApplicationTab(service, context);
		// appTab.setWidth("100%");
		//
		// tabs.add(appTab, constants.appMgmt());
		//
		// serviceTab = new ServiceTab(service, context);
		// serviceTab.setWidth("100%");
		// tabs.add(serviceTab, constants.serviceMgmt());
		//
		// SecurityTab secTab = new SecurityTab(service, context);
		// secTab.setWidth("100%");
		// tabs.add(secTab, constants.securityMgmt());
		//
		//		
		// ConfigTab configTab = new ConfigTab(service , context);
		// configTab.setWidth("100%");
		// tabs.add(configTab, constants.configMgmt());
		// vPanel.add(tabs);
		panel = new SimplePanel();
		panel.setWidth("100%");

		LoginPanel login = new LoginPanel(constants, service, context);
		login.setContentChangeListener(this);
		panel.setWidget(login);

		RootPanel.get().add(panel);

	}

	
	public void onContentChanged(String source, String dest, HashMap props) {
		if (source.equals(CatalogContentConstants.LOGIN_PANEL)
				&& dest.equals(CatalogContentConstants.MAIN_PANEL)) {
			MainPanel mainPanel = new MainPanel(constants, service, context);
			mainPanel.setContentChangeListener(this);
			panel.setWidget(mainPanel);
			return;
		}
		
		if (source.equals(CatalogContentConstants.MAIN_PANEL)
				&& dest.equals(CatalogContentConstants.LOGIN_PANEL)) {
			LoginPanel p = new LoginPanel(constants, service, context);
			p.setContentChangeListener(this);
			panel.setWidget(p);
			return;
		}

	}
}
